<template>
  <el-dialog
      :title="{ add: '新增角色', edit: '编辑角色' }[mode]"
    width="600px"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRolePost,sysRolePut } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      mode: '', // add新增 edit:编辑
      form: {
        name: '', //  string  必须    角色名称
        description: '' //  string  必须    角色描述
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        description: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 确定按钮点击
    //  全局表单验证
    submit() {
      this.$refs.form.validate(async(result) => {
        console.log(result)
        if (result) {

if(this.mode==='add'){

          await sysRolePost(this.form)
          //  提升
          this.$message.success('新增成功')


}else if(this.mode==='edit'){
    await sysRolePut(this.form)
    this.$message.success('编辑成功')
}

          // 关闭弹框
          this.isShow = false
          //  刷新父级列表数据
          this.$emit('getData')
        }
      })
    },
    // 关闭时清空表单验证与重置表单数据
    closeEvent() {
      //  重置表单清除表单验证
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    
  }
}
</script>
<style></style>
